import logging
import telnetlib
import time
import re

def deal_sleep(l):
    # time.sleep(l)
    pass

class TelnetObj():
    _ip = ''
    _usernm = ''
    _passwd = ''
    # _tn = None

    def __init__(self,ip,passwd):
        self._ip = ip;
        self._passwd = passwd;
        self._tn = telnetlib.Telnet()
        print('打开%s:23端口' % ip)
        # self._tn.open(self._ip, 23) 
        if not self.login():
            print('登录失败')
            # return '登录失败'

    # 此函数实现telnet登录主机
    def login(self):
        # time.sleep(5)
        deal_sleep(5)
        print('登录%s' % (self._ip))
        # 延时一秒再收取返回结果，给服务端足够响应时间
        # time.sleep(1)
        deal_sleep(1)
        # 获取登录结果
        print('输入密码%s' % self._passwd)
        # read_very_eager()获取到的是的是上次获取之后本次获取之前的所有输出
        # print(command_result)
        print('登录成功')
        print('特权获取成功')
        return True;

    # 此函数实现执行传过来的命令，并输出其执行结果
    def _execute_one_command(self,tn,command):
        # 执行命令
        print('执行命令%s' % command)
        # 获取命令结果
        return '执行成功\n'

    # 执行多条命令
    def _execute_many_commands(self,tn,commands):

        for command in commands:
            # 执行命令
            print('执行命令%s' % command)
            # self._execute_one_command(tn,command)

    # 执行配置命令
    def execute_conf_commands(self,commands):
        # try:
            # with telnetlib.Telnet(self._ip, 23) as tn:
                # 登录
                # if not self._login(tn,self._ip,self._passwd):
                    # return '登录失败'
                # 执行进入配置窗口的命令
        
            # 延时一秒再收取返回结果，给服务端足够响应时间
        # time.sleep(1)
        deal_sleep(1)
            # 获取登录结果
            # read_very_eager()获取到的是的是上次获取之后本次获取之前的所有输出
        print('执行conf term')        

        # 执行用户输入的多条命令
        self._execute_many_commands(self._tn,commands)

        print('执行exit')
        return '执行成功\n'
        # except e:
        #     return '网络连接失败'
        
    # 执行一条命令并缓慢的获得输出
    def execute_query_command_slowly(self, command):
        # 执行命令
        print('执行%s' % command);
        # 等待一个较长的时间，确保命令都执行完毕了
        time.sleep(10)
        # 获取命令结果
        return '执行成功\n'
    # 执行查询命令
    def execute_query_command(self ,command):
        # 最终返回结果
        # try:
        # tn = telnetlib.Telnet()
        # tn.open(self._ip, 23) 
        # 登录
        # if not self._login(tn,self._ip,self._passwd):
        #     return '登录失败'
        # res = self._execute_one_command(self._tn,command);
        # print(res)
        # 处理成列表，方便增减句子
        # res = res.split('\n')

        return self._execute_one_command(self._tn,command);
        # newRes = []
        # # 发送回车，返回新的一行
        # if 'show ip interface' in command:
        #     for ints in int_list:
        #         temp = self._execute_one_command(self._tn,command + ' ' + ints)
        #         res += temp
            
        # elif 'access-list' in  command:
        #     res = self._execute_one_command(self._tn,command);
        
        
        # # 当查询的结果没有显示完全的时候
        # while '--More--' in res:
        #     # 去除more
        #     res = res[:-1];
        #     # 发送回车，返回新的一行
        #     temp = self._execute_one_command(self._tn,'enter')
        #     print(temp)
        #     # 把新的一行添加到列表末尾
        #     res.append(temp)
        # print(res)
        # # 返回完整句子
        
        # except BaseException as e:
        #     print(e)
        #     print('%s网络连接失败' % host_ip)

if __name__ == '__main__':
    host_ip = '172.16.0.2'
    username = 'CISCO'
    password = 'CISCO'
    command = 'show int '
    telnetObj = TelnetObj(host_ip,password)
    telnetObj.execute_conf_commands(['ip access-list standard test1','10 deny all','exit'])
    # print(telnetObj.execute_query_command(command));
    # time.sleep(3)
    # print(telnetObj.execute_query_command('show ip access-list'));
    # print(telnetObj.execute_query_command('show int f0/1'));

